package cn.kgc.kgde.bookstore.service.impl;

import cn.kgc.kgde.bookstore.dto.CartDTO;
import cn.kgc.kgde.bookstore.entity.Book;
import cn.kgc.kgde.bookstore.entity.Cart;
import cn.kgc.kgde.bookstore.entity.Cartitem;
import cn.kgc.kgde.bookstore.entity.User;
import cn.kgc.kgde.bookstore.mapper.CartMapper;
import cn.kgc.kgde.bookstore.mapper.CartitemMapper;
import cn.kgc.kgde.bookstore.service.CartService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.toolkit.MPJWrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author myt
 * @since 2022-05-23
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Resource
    private CartMapper cartMapper;

    //购物车详情展示--cart-catrItem-book三表联查
    @Override
    public IPage<CartDTO> listCart(String userCode) {

        IPage<CartDTO> iPage = cartMapper.selectJoinPage(new Page<>(),CartDTO.class, MPJWrappers.lambdaJoin()
                .selectAll(Cart.class)
                .selectAll(Cartitem.class)
                .selectAll(Book.class)
                .innerJoin(Cartitem.class,Cartitem::getCartId,Cart::getCartId)
                .innerJoin(Book.class,Book::getId,Cartitem::getBookId)
                .eq(Cart::getUserCode,userCode));
        iPage.getRecords().forEach(System.out::println);
        for (CartDTO record : iPage.getRecords()) {
            record.setCost(record.getPrice()* record.getQuantity());
            System.out.println("record = " + record);
        }
        return iPage;
    }

    /**
     * 根据userCode查询购物车
     * @param userCode
     * @return
     */
    @Resource
    private CartitemMapper cartitemMapper;
    @Override
    public void addCart(String userCode,Cartitem cartitem) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userCode",userCode);
        Cart cart=this.cartMapper.selectOne(wrapper);
        System.out.println(cart+"cart---------------------------");
        System.out.println(userCode+"user------------------------");
        QueryWrapper wa= new QueryWrapper();
        wa.eq("cartId",cart.getCartId());
        List<Cartitem> cartitemList = this.cartitemMapper.selectList(wa);

//        System.out.println("cartitemList"+cartitemList);
        //循环 购物车当前数据
        //然后根据 bookid  判断
        for (Cartitem carItem2 : cartitemList){
            if (carItem2.getCartId().equals(cart.getCartId())){
                if (cartitem.getBookId().equals(carItem2.getBookId())){
                    //购物车内商品相同
                    //修改购物信息 -- 购物数量 ++
                    System.out.println("有相同商品，，修改数量");
                    Integer one = carItem2.getQuantity();
                    Integer quantity = one + cartitem.getQuantity();
                    carItem2.setQuantity(quantity);
                    this.cartitemMapper.updateById(carItem2);
                    System.out.println("修改购物数量成功------------------------------------------------------------------------------------");
                    return;
                }
            }
        }
        cartitem.setCartId(cart.getCartId());
        this.cartitemMapper.insert(cartitem);
        System.out.println("无相同数据，添加新的商品");
        return;
    }




}
